﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class BillingCredenceBLL
    {
        private static readonly IBillingCredenceDA billingCredenceDA = DataAccess.CreateBillingCredenceDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private string strCurrentWhName;

        public BillingCredenceBLL()
        {
            this.pubParasInfo = null;
            this.strCurrentWhName = "";
        }

        public BillingCredenceBLL(string currentWH)
        {
            this.pubParasInfo = null;
            this.strCurrentWhName = "";
            this.strCurrentWhName = currentWH;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public BillingCredenceBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            this.strCurrentWhName = "";
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public DataSet GetAllInfo()
        {
            return billingCredenceDA.GetAllBillingCredence(this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetBillingCredenceByQueryList(BillingCredenceQueryEntity billingCredenceQueryEntity)
        {
            BillingCredenceQueryEntity billingCredenceQuery = billingCredenceQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(billingCredenceQuery.DocID))
            {
                sqlWhere.Add(" BC.DOC_ID " + billingCredenceQuery.DocID);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.DocType))
            {
                sqlWhere.Add(" BC.DOC_TYPE " + billingCredenceQuery.DocType);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.DueFromTo))
            {
                sqlWhere.Add(" BC.DUE_FROM_TO " + billingCredenceQuery.DueFromTo);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.BillingObject))
            {
                sqlWhere.Add(" BC.BILLING_OBJECT " + billingCredenceQuery.BillingObject);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.BillingObjectID))
            {
                sqlWhere.Add(" BC.BILLING_OBJECT_ID " + billingCredenceQuery.BillingObjectID);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.CredenceType))
            {
                sqlWhere.Add(" BC.CREDENCE_TYPE " + billingCredenceQuery.CredenceType);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.CreatedDate))
            {
                sqlWhere.Add(billingCredenceQuery.CreatedDate.Replace("@CreatedDate@", "BC.CREATED_DATE"));
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.IsStatement))
            {
                sqlWhere.Add(" ISNULL(BC.IS_STATEMENT,'') " + billingCredenceQuery.IsStatement);
            }
            return billingCredenceDA.GetBillingCredenceByQueryList(sqlWhere, billingCredenceQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetBillingCredenceByStatementID(string statementID)
        {
            return billingCredenceDA.GetBillingCredenceByStatementID(statementID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetBillingCredenceGroupByQueryList(BillingCredenceQueryEntity billingCredenceQueryEntity)
        {
            BillingCredenceQueryEntity billingCredenceQuery = billingCredenceQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(billingCredenceQuery.DocID))
            {
                sqlWhere.Add(" BC.DOC_ID " + billingCredenceQuery.DocID);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.DueFromTo))
            {
                sqlWhere.Add(" BC.DUE_FROM_TO " + billingCredenceQuery.DueFromTo);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.BillingObject))
            {
                sqlWhere.Add(" BC.BILLING_OBJECT " + billingCredenceQuery.BillingObject);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.BillingObjectID))
            {
                sqlWhere.Add(" BC.BILLING_OBJECT_ID " + billingCredenceQuery.BillingObjectID);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.CredenceType))
            {
                sqlWhere.Add(" BC.CREDENCE_TYPE " + billingCredenceQuery.CredenceType);
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.CreatedDate))
            {
                sqlWhere.Add(billingCredenceQuery.CreatedDate.Replace("@CreatedDate@", "BC.CREATED_DATE"));
            }
            if (!string.IsNullOrEmpty(billingCredenceQuery.IsStatement))
            {
                sqlWhere.Add(" ISNULL(BC.IS_STATEMENT,'') " + billingCredenceQuery.IsStatement);
            }
            return billingCredenceDA.GetBillingCredenceGroupByQueryList(sqlWhere, billingCredenceQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string UpdateBillingCredencePrintDate(List<BillingCredenceInfo> billingCredenceInfos)
        {
            int result = 0;
            string where = string.Empty;
            decimal totalAmount = 0M;
            decimal totalPrintedAmount = 0M;
            DateTime dt = DateTime.Now;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < billingCredenceInfos.Count; i++)
                {
                    totalAmount += billingCredenceInfos[i].BillingAmount;
                    where = " WHERE 1= 1  ";
                    if (!string.IsNullOrEmpty(billingCredenceInfos[i].CredenceType))
                    {
                        where = where + " AND BC.CREDENCE_TYPE + BC.DOC_TYPE = '" + billingCredenceInfos[i].CredenceType + "'";
                    }
                    if (!string.IsNullOrEmpty(billingCredenceInfos[i].DocID))
                    {
                        where = where + " AND BC.DOC_ID = '" + billingCredenceInfos[i].DocID + "'";
                    }
                    if (!string.IsNullOrEmpty(billingCredenceInfos[i].DueFromTo))
                    {
                        where = where + " AND BC.DUE_FROM_TO = '" + billingCredenceInfos[i].DueFromTo + "'";
                    }
                    if (!string.IsNullOrEmpty(billingCredenceInfos[i].DueFromTo))
                    {
                        where = where + " AND BC.COST_TYPE = '" + billingCredenceInfos[i].CostType + "'";
                    }
                    if (!string.IsNullOrEmpty(billingCredenceInfos[i].ChargeUnit))
                    {
                        where = where + " AND BC.CHARGE_UNIT = '" + billingCredenceInfos[i].ChargeUnit + "'";
                    }
                    if (!string.IsNullOrEmpty(billingCredenceInfos[i].Remark))
                    {
                        where = where + " AND BC.REMARK = '" + billingCredenceInfos[i].Remark + "'";
                    }
                    object CS00002 = where;
                    where = string.Concat(new object[] { CS00002, " AND BC.FIXED_RATE = '", billingCredenceInfos[i].FixedRate, "'" });
                    List<BillingCredenceInfo> billingCredencePrintInfos = billingCredenceDA.GetBillingCredenceListByWhere(dataBase, tran, where, this.dataBaseID[0], this.pubParasInfo);
                    if ((billingCredencePrintInfos != null) && (billingCredencePrintInfos.Count > 0))
                    {
                        for (int j = 0; j < billingCredencePrintInfos.Count; j++)
                        {
                            BillingCredenceInfo billingCredenceInfo = billingCredenceDA.GetBillingCredenceByID(dataBase, tran, billingCredencePrintInfos[j].BillingCredenceID, this.dataBaseID[0], this.pubParasInfo);
                            if (billingCredenceInfo != null)
                            {
                                billingCredenceInfo.PrintedDate = dt;
                                billingCredenceInfo.UpdatedBy = this.pubParasInfo.UserID;
                                billingCredenceInfo.UpdatedDate = DateTime.Now;
                                result += billingCredenceDA.UpdateBillingCredenceByPrint(dataBase, tran, billingCredenceInfo, this.dataBaseID);
                                totalPrintedAmount += billingCredenceInfo.BillingAmount;
                            }
                        }
                    }
                }
                if (totalPrintedAmount != totalAmount)
                {
                    throw new Exception("打印与查询金额数据不一致，打印标志更改失败");
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (result > 0)
            {
                return result.ToString();
            }
            return "";
        }
    }
}

